﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

# region Add Office namespace
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Word;
# endregion Add Office namespace


namespace WPFVS2010
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class VS2010 : System.Windows.Window
    {
        public VS2010()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
           
        }
 
        /// <summary>
        /// Display the data in excel
        /// </summary>
        /// <param name="accounts"></param>
        void DisplayInExcel(IEnumerable<Account> accounts)
        {
            var excelApp = new Microsoft.Office.Interop.Excel.Application();

            // make the object visible
            excelApp.Visible = true;

            // Add a new worksheet
            excelApp.Workbooks.Add();

            // Uses a single worksheet. The explicit type casting is remvoed in a later procedure.
            Microsoft.Office.Interop.Excel._Worksheet worksheet =
                (Microsoft.Office.Interop.Excel.Worksheet)excelApp.ActiveSheet;

            worksheet.Cells[1, "A"] = "ID";
            worksheet.Cells[1, "B"] = "Balance";

            var row = 1;
            foreach (var acct in accounts)
            {
                row++;
                worksheet.Cells[row, "A"] = acct.ID;
                worksheet.Cells[row, "B"] = acct.Balance;
            }

            worksheet.Columns[1].AutoFit();
            worksheet.Columns[2].AutoFit();

            worksheet.get_Range("A1", "B3").AutoFormat(
                Microsoft.Office.Interop.Excel.XlRangeAutoFormat.xlRangeAutoFormatClassic2
                );

            // Put the spreadsheet contens on the clipboard.
            // The Copy method has one optional parameter
            // for specifying a destination.
            // Because no argument is sent, the 
            // destination is clipboard.
            worksheet.get_Range("A1:B3").Copy();

            // Earlier version of C#
            // ((Excel.Range)worksheet.Columns[1]).AutoFit();            
        }


        void CreateIconInWordDoc()
        {
            var wordApp = new Microsoft.Office.Interop.Word.Application();
            wordApp.Visible = true;
            wordApp.Documents.Add();
            // PasteSpecial has seven reference parameters, all of which
            // are optional. This example uses named arguments to spcify 
            // values for two of the parameters. Although these are
            // reference parameters, you do not need to use the ref keyword,
            // or to create variables to send in as arguments. You can send the value directly.
            wordApp.Selection.PasteSpecial(Link: true, DisplayAsIcon: true);
            // Earlier version of C#
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            # region Create account
            var bankAccounts = new List<Account>
            {
                new Account {
                    ID = 12345,
                    Balance = 500.12
                },
                new Account {
                    ID = 12346,
                    Balance = -200.50
                }
            };
            #endregion Create account 
            DisplayInExcel(bankAccounts);
            CreateIconInWordDoc();
        }
    }

    public class Account
    {
        public int ID { get; set; }
        public double Balance { get; set; }
    }
}
